package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.api.service.XcqdService;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.crm.entity.*;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *功能描述
 * @author caijy
 * @description
 * @datetime 2019-11-8 0008 17:18
 * @return
 */
@RestController
@RequestMapping("api/wx/xcqd")
public class XcqdController extends ControllerSupport {

    @Autowired
    XcqdService xcqdService;

    @RequestMapping(value = "saveData")
    public ResponseData xchbSave(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        SysUser currentUser = wapper.getSysUser();
        MmrgT mmrgT = new MmrgT();
        MmrdT mmrdT = new MmrdT();
        List<MmrhT> MmrhTs = new ArrayList<>();
        List<MmriT> MmriTs = new ArrayList<>();
        List<MmrqT> mmrqTS = new ArrayList<>();
        ResponseData data = new ResponseData();
        try {

            JSONObject inJson = JSONObject.parseObject(inDto.getString("obj"));
            inDto.put("MMRFENT", currentUser.getDBC());
            inDto.put("MMRF007", currentUser.getID());//员工编号

            mmrgT.setMMRGENT(currentUser.getDBC());
            mmrgT.setMMRGDOCNO("WXHB" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
            mmrgT.setMMRGSEQ(BigDecimal.ONE);
            mmrgT.setMMRGDOCDATE(LocalDate.now());
            mmrgT.setMMRG001(inJson.getString("MMRFDOCNO"));
            mmrgT.setMMRG002(inJson.getBigDecimal("MMRFSEQ"));
            mmrgT.setMMRG003(inJson.getString("MMRF003"));
            mmrgT.setMMRG004(inJson.getString("MMRF004"));
            mmrgT.setMMRG005(currentUser.getID());
            Date MMRF009 = inJson.getDate("MMRF009");
            if (MMRF009 != null) {
                mmrgT.setMMRG006(CommonUtils.dateToLocalDate(MMRF009));
            }else{
                mmrgT.setMMRG006(LocalDate.now());
            }
            mmrgT.setMMRG007(CommonUtils.dateToLocalDate(inJson.getDate("MMRG007")));
            mmrgT.setMMRG008(inJson.getString("MMRG008"));
            mmrgT.setMMRG009(inJson.getString("MMRG009"));// 图片地址
            mmrgT.setMMRG010(inJson.getString("MMRG010"));
            mmrgT.setMMRG011(" ");//客户意见
            mmrgT.setMMRG012(" "); // 签名地址
            mmrgT.setMMRG013(inJson.getString("MMRG013"));
            mmrgT.setMMRG100(inJson.getString("MMRG100"));
            mmrgT.setMMRG101(inJson.getString("MMRG101"));//差旅时数
            mmrgT.setMMRG102(inJson.getString("MMRG102"));
            mmrgT.setMMRG104(inJson.getBigDecimal("MMRG104"));//报工时数
            mmrgT.setMMRGACTI("Y");
            mmrgT.setMMRGDATE(LocalDate.now());
            mmrgT.setMMRGTIME(DateUtils.getTime());
            mmrgT.setMMRGUSER(currentUser.getID());

            // 修改 mmrd表 状态为7已签到
            mmrdT.setMMRDENT(currentUser.getDBC());
            mmrdT.setMMRDSEQ(inJson.getBigDecimal("MMRDSEQ"));
            mmrdT.setMMRDDOCNO(inJson.getString("MMRF001"));
            mmrdT.setMMRD020("7");

            JSONArray mmrhJA = inJson.getJSONArray("pjlist");

            GwamT gwam = new GwamT();
            for (int i = 0, j = mmrhJA.size(); i < j; i++) {
                JSONObject item = mmrhJA.getJSONObject(i);
                MmrhT mmrh = new MmrhT();
                mmrh.setMMRHENT(currentUser.getDBC());
                mmrh.setMMRHDOCNO(mmrgT.getMMRGDOCNO());
                mmrh.setMMRHSEQ(BigDecimal.valueOf(i+1));
                mmrh.setMMRH001(item.getString("MMRH001"));
                mmrh.setMMRH002(item.getString("MMRH002"));
                mmrh.setMMRH003(item.getString("MMRS004"));
                mmrh.setMMRH004(item.getBigDecimal("MMRH004"));
                mmrh.setMMRH005(item.getBigDecimal("MMRS009"));
                mmrh.setMMRH006(item.getString("MMRH002").equals("1") ? mmrh.getMMRH004().multiply(mmrh.getMMRH005()) : BigDecimal.ZERO);

                gwam = gwam.selectById(item.getString("MMRS004"));
                if (gwam != null && CommonUtils.isNum(gwam.getGWAM012()) && CommonUtils.isNum(gwam.getGWAM013())) {
                    mmrh.setMMRH007(CommonUtils.addDays(LocalDate.now(),
                            Integer.parseInt(gwam.getGWAM012())*30+Integer.parseInt(gwam.getGWAM013()))); //配件三包到期日期
                }
                mmrh.setMMRH008(item.getString("MMRS010"));
                mmrh.setMMRH009(item.getString("MMRH009"));
                mmrh.setMMRHACTI("Y");
                mmrh.setMMRHDATE(LocalDate.now());
                mmrh.setMMRHTIME(DateUtils.getTime());
                mmrh.setMMRHUSER(currentUser.getID());
                MmrhTs.add(mmrh);
            }

            JSONArray expenses = inJson.getJSONArray("expensesList");

            for (int i = 0, j = expenses.size(); i < j; i++) {
                JSONObject item = expenses.getJSONObject(i);
                MmriT mmri = new MmriT();
                mmri.setMMRIENT(currentUser.getDBC());
                mmri.setMMRIDOCNO(mmrgT.getMMRGDOCNO());
                mmri.setMMRISEQ(BigDecimal.valueOf(i+1));
                mmri.setMMRI001(item.getString("MMRI001"));
                mmri.setMMRI002(item.getBigDecimal("MMRI002"));
                mmri.setMMRI003(item.getBigDecimal("MMRI003"));
                mmri.setMMRIACTI("Y");
                mmri.setMMRIDATE(LocalDate.now());
                mmri.setMMRITIME(DateUtils.getTime());
                mmri.setMMRIUSER(currentUser.getID());
                MmriTs.add(mmri);
            }

            //
            if(inJson.getString("MMRD013")!=null){
                String MMRD013s = inJson.getString("MMRD013");
                String[] MMRD013Array = MMRD013s.split(",");

                int seq = 1;
                for(String MMRD013:MMRD013Array){
                    if (StringUtils.isNotEmpty(MMRD013)){
                        MmrqT mmrqT = new MmrqT();
                        mmrqT.setMMRQDOCNO(mmrgT.getMMRGDOCNO());
                        mmrqT.setMMRQENT(currentUser.getDBC());
                        mmrqT.setMMRQSEQ(seq);
                        mmrqT.setMMRQ001(MMRD013);

                        mmrqT.setMMRQACTI("Y");
                        mmrqT.setMMRQDATE(LocalDate.now());
                        mmrqT.setMMRQTIME(DateUtils.getTime());
                        mmrqT.setMMRQUSER(currentUser.getID());
                        mmrqT.setMMRQGRUP(currentUser.getORG_ID());
                        mmrqT.setMMRQORIG(currentUser.getORG_ID());
                        mmrqT.setMMRQORIU(currentUser.getID());

                        mmrqTS.add(mmrqT);
                        seq ++;
                    }
                }
            }

            xcqdService.saveData(mmrgT,MmrhTs,MmriTs,mmrdT,mmrqTS);

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }
}
